package P4树形结构;

import java.util.ArrayList;
import java.util.Comparator;

public class TestMap {
    public static void main(String[] args) throws InterruptedException {
        ArrayList<String> words = new ArrayList<>();
        FileOperation.readFile("pride-and-prejudice.txt", words);
        words.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });

        System.out.println("total = " + words.size());

        TreeMap<String, Integer> map1 = new TreeMap();
        test("TreeMap", words, map1);

        Thread.sleep(2000);

        AVLTreeMap<String, Integer> map2 = new AVLTreeMap<>();
        test("AVLTreeMap", words, map2);
        System.out.println(map2.isBST());
        System.out.println(map2.isBalanced());

        Thread.sleep(2000);
        HashMap<String, Integer> map3 = new HashMap<>();
        test("HashMap", words, map3);

        Trie trie = new Trie();
        testTrie("Trie",words,trie);
    }

    private static void testTrie(String name, ArrayList<String> words, Trie trie) {
        System.out.println(name);
        long start = System.currentTimeMillis();
        for (String word : words) {
            trie.add(word);
        }
        System.out.println("size = " + trie.size());
        System.out.println("count = " + trie.count());
        System.out.println("pride :" + trie.wordCount("pride"));
        for (String word : words) {
            trie.contains(word);
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start + "ms");
    }

    private static void test(String name, ArrayList<String> words, Map<String, Integer> map) {
        System.out.println(name);
        long start = System.currentTimeMillis();
        for (String word : words) {
            map.put(word, map.getOrDefault(word, 0) + 1);
        }
        System.out.println("size = " + map.size());
        System.out.println("pride :" + map.get("pride"));

        for (String word : words) {
            map.get(word);
        }

        long end = System.currentTimeMillis();
        System.out.println(end - start + "ms");
    }
}